package com.ylbh.modules.balancelog.controller;

import java.math.BigDecimal;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.balancelog.dto.UserBalanceMoneyDTO;
import com.ylbh.common.pojo.member.dto.select.RevenueLogSelectDTO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.balancelog.service.ShoppingBalanceLogService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 用户余额变动表 主要记录用户的余额变动信息
 *
 * @author czx
 * @date 2019-09-03 14:25:10
 */
@RestController
@RequestMapping("balanceLog")
public class ShoppingBalanceLogController {
    @Autowired
    private ShoppingBalanceLogService shoppingBalanceLogService;

    @ApiOperation("余额变动")
    @GetMapping("queryBalanceLogList")
    @RequiresPermissions("balanceLog:queryBalanceLogList")
    public CommonResult queryBalanceLogList(RevenueLogSelectDTO pageDTO) {
        PageUtils page = shoppingBalanceLogService.queryBalanceLogList(pageDTO);
        return Ref.ok(page);
    }

    @SysLog("修改用户的佣金")
    @ApiOperation("修改用户的佣金")
    @PostMapping("modifyUserCommissionMoney")
    @RequiresPermissions("balanceLog:modifyUserCommissionMoney")
    public CommonResult modifyUserCommissionMoney(UserBalanceMoneyDTO userBalanceMoneyDTO) {
        String msg =shoppingBalanceLogService.modifyUserCommissionMoney(userBalanceMoneyDTO);
        if (msg.equals("当前用户佣金余额不足") || msg.equals("操作失败")) {
            return Ref.error(msg);
        }
        return Ref.ok(msg);
    }

    @ApiOperation("查询用户当前的佣金")
    @RequiresPermissions("balanceLog:getCommissionMoneyByUserName")
    @GetMapping("getCommissionMoneyByUserName/{name}")
    public CommonResult getCommissionMoneyByUserName(@PathVariable String name) {
        BigDecimal commissionMoney = shoppingBalanceLogService.getCommissionMoneyByUserName(name);
        return Ref.ok(commissionMoney);
    }

    @ApiOperation("系统管理余额记录")
    @GetMapping("queryAdminAndBalanceLog")
    @RequiresPermissions("balanceLog:queryAdminAndBalanceLog")
    public CommonResult queryAdminAndBalanceLog(RevenueLogSelectDTO dto) {
        PageUtils page = shoppingBalanceLogService.queryAdminAndBalanceLog(dto);
        return Ref.ok(page);
    }

    @ApiOperation("余额变动导出")
    @RequestMapping("balanceRecordExcel")
    public CommonResult balanceRecordExcel(HttpServletRequest request, HttpServletResponse response) throws NormalException {
        shoppingBalanceLogService.balanceRecordExcel(request, response);
        return Ref.ok();
    }
}
